package com.smarth.zhizulian.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextPaint;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.bean.activity.ActivityShare;
import com.smarth.zhizulian.bean.lucky.LuckyShareIncome;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.popupwindow.KumaDialog;

import org.simple.eventbus.EventBus;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by 18865 on 2018/1/23.
 */

public class KumaDialogFactory {
//    View vv = new View(context);
//        vv.setBackgroundColor(Color.BLACK);

    /**
     * 收徒页面活动结束后点击去告诉好友
     */
    public static KumaDialog buildActivityShareDialog(final Activity context, final ActivityShare activityShare) {
        return new KumaDialog(context, 610, 492)
                .setBackgound(R.drawable.fenxiangweixinpengyouquan)
                .addWidget(new KumaDialog.DialogView(156, 204)
                                .setMarginLeft(85)
                                .setMarginTop(78)
                                .setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
//                                KumaToast.show(context, "微信");
//                                ShareUtils.shareToWechatImage(activityShare.get);
                                        ShareUtils.shareActivityResult(context, activityShare.getMoeny(), activityShare.getShareUrl(), Wechat.NAME);
                                    }
                                })
                )
                .addWidget(new KumaDialog.DialogView(156, 204)
                        .setMarginLeft(369)
                        .setMarginTop(78)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                KumaToast.show(context, "朋友圈");
                                ShareUtils.shareActivityResult(context, activityShare.getMoeny(), activityShare.getShareUrl(), WechatMoments.NAME);
                            }
                        }))
                .addWidget(new KumaDialog.DialogView(76, 76)
                        .setMarginTop(422)
                        .setMarginLeft(266)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        })
                );
    }

    /**
     * 活动结果
     *
     * @param context
     * @param num
     * @param money
     * @return
     */
    public static KumaDialog buildActivityResultDialog(final Activity context, String num, String money) {
        TextView textView = new TextView(context);
        textView.setTextColor(Color.WHITE);
        textView.setText(Html.fromHtml("获得邀请活动排名<font color=\"#fafb3f\">第" + num + "名</font>"));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 38);
        TextPaint tp = textView.getPaint();
        tp.setFakeBoldText(true);

        TextView textView2 = new TextView(context);
        textView2.setText(money + "元");
        textView2.setTextColor(Color.parseColor("#fd3b29"));
        textView2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 92);
        TextPaint tp2 = textView2.getPaint();
        tp2.setFakeBoldText(true);

        return new KumaDialog(context, 619, 1062)
                .setBackgound(R.drawable.gongxinin)
                .addWidget(new KumaDialog.DialogView(textView)
                        .setMarginTop(436)
                        .setCenterHorizontal(true)
                )
                .addWidget(new KumaDialog.DialogView(textView2)
                        .setMarginTop(655)
                        .setCenterHorizontal(true)
                )
                .addWidget(new KumaDialog.DialogView(49, 49)
                        .setRight(true)
                        .setMarginRight(15)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                )
                .addWidget(new KumaDialog.DialogView(354, 100)
                        .setBottom(true)
                        .setMarginBottom(10)
                        .setCenterHorizontal(true)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                KumaHttp.getService().activityShare()
                                        .compose(RxCompos.<ActivityShare>getBELoginCompos())
                                        .subscribe(new ProgressObserver<ActivityShare>(context) {
                                            @Override
                                            public void onNext(ActivityShare activityShare) {
                                                buildActivityShareDialog(context, activityShare).show();
                                            }
                                        });
                            }
                        })
                )
                ;
    }

    /**
     * 活动开始弹窗
     *
     * @param context
     * @return
     */
    public static KumaDialog buildActivityStartDialog(final Activity context) {
        return new KumaDialog(context, 721, 1041)
                .setBackgound(R.drawable.jinruhuodong)
                .addWidget(new KumaDialog.DialogView(388, 76)
                                .setCenterHorizontal(true)
                                .setMarginBottom(188)
                                .setBottom(true)
                                .setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (!UserUtils.isLogin()) {
                                            LoginActivity.open(context);
                                        } else {
//                                    KumaToast.show(context, "进入活动");
                                            AgentWebActivity.openForActivity(context);
                                        }
                                    }
                                })
                )
                .addWidget(new KumaDialog.DialogView(76, 76)
                        .setBottom(true)
                        .setCenterHorizontal(true)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                );
    }

    /**
     * 开抢红包提示
     *
     * @return
     */
    public static KumaDialog buildHongBaoRainStartDialog(Activity context, View.OnClickListener onClickStart) {
        return new KumaDialog(context, 560, 560)
                .setBackgound(R.drawable.kaiqianghongbaotishi)
                .addWidget(new KumaDialog.DialogView(40, 40)
                        .setRight(true)
                        .setMarginRight(20)
                        .setMarginTop(20)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                )
                .addWidget(new KumaDialog.DialogView(360, 90)
                        .setCenterHorizontal(true)
                        .setBottom(true)
                        .setMarginBottom(50)
                        .setOnClickListener(onClickStart)
                );
    }

    /**
     * 抢到红包弹窗
     *
     * @param context
     * @return
     */
    public static KumaDialog buildGetHongBaoRainDialog(final Activity context, String num, String coin) {
        if (num.equals("0")) {
            return buildGetNoHongBaoRainDialog(context);
        }
        GifImageView gifImageView = new GifImageView(context);
        gifImageView.setImageResource(R.drawable.caishen);

        ImageView ivback = new ImageView(context);
        ivback.setImageResource(R.drawable.hongbaoyu_tanchaung_bg);

        TextView textView = new TextView(context);
        textView.setTextColor(Color.parseColor("#fffc02"));
        textView.setText("恭喜您");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 44);

        TextView textView2 = new TextView(context);
        textView2.setTextColor(Color.parseColor("#fffc02"));
        textView2.setText("获得" + num + "个现金红包");
        textView2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 44);

        ImageView ivCancel = new ImageView(context);
        ivCancel.setImageResource(R.drawable.hongbaoyu_tanchaung_cha);

        LinearLayout ll = new LinearLayout(context);
        ll.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        TextView text = new TextView(context);
        text.setText("共");
        text.setTextColor(Color.parseColor("#fffc02"));
        text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 44);
//        LinearLayout.LayoutParams lpText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ll.addView(text);
        TextView coinText = new TextView(context);
        coinText.setTextColor(Color.WHITE);
        coinText.setTextSize(TypedValue.COMPLEX_UNIT_PX, 90);
        coinText.setText(coin + "金币");
        LinearLayout.LayoutParams lpCoinText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ll.addView(coinText, lpCoinText);

        return new KumaDialog(context, 750, 1334)
                .addWidget(new KumaDialog.DialogView(gifImageView, 452, 386)
                        .setCenterHorizontal(true)
                        .setMarginTop(238)
                )
                .addWidget(new KumaDialog.DialogView(ivback, 582, 838)
                        .setCenterHorizontal(true)
                        .setMarginTop(272)
                )
                .addWidget(new KumaDialog.DialogView(300, 82)
                        .setCenterHorizontal(true)
                        .setMarginTop(1016)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                KumaDialogFactory.buildShareDialog(context, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ShareUtils.shareHongBao(context, Wechat.NAME);
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ShareUtils.shareHongBao(context, WechatMoments.NAME);
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ShareUtils.shareHongBao(context, QQ.NAME);
                                    }
                                }).show();
                            }
                        })
                )
                .addWidget(new KumaDialog.DialogView(textView)
                        .setCenterHorizontal(true)
                        .setMarginTop(560)
                )
                .addWidget(new KumaDialog.DialogView(textView2)
                        .setCenterHorizontal(true)
                        .setMarginTop(613)
                )
                //共1500金币
                .addWidget(new KumaDialog.DialogView(ll)
                        .setCenterHorizontal(true)
                        .setMarginTop(678)
                )
                .addWidget(new KumaDialog.DialogView(ivCancel, 51, 49)
                        .setRight(true)
                        .setMarginTop(70)
                        .setMarginRight(70)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                );
    }

    /**
     * 捡漏红包
     *
     * @param context
     * @return
     */
    public static KumaDialog buildLeftHongBaoDialog(final Activity context, final long coin, final long id) {
        TextView textView = new TextView(context);
        textView.setTextColor(Color.WHITE);
        textView.setText("+" + coin + "金币");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80);
        TextPaint tp = textView.getPaint();
        tp.setFakeBoldText(true);
        return new KumaDialog(context, 750, 1334)
                .setBackgound(coin < 30 ? R.drawable.jianlou_bg : R.drawable.jianlou_bg02)
                .addWidget(new KumaDialog.DialogView(40, 40)
                        .setRight(true)
                        .setMarginTop(70)
                        .setMarginRight(70)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                )
                .addWidget(new KumaDialog.DialogView(textView)
                        .setCenterHorizontal(true)
                        .setMarginTop(650)
                )
                .addWidget(new KumaDialog.DialogView(306, 86)
                        .setBottom(true)
                        .setCenterHorizontal(true)
                        .setMarginBottom(333)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ShareUtils.shareHongBaoFriend(context, id, WechatMoments.NAME, !(coin < 30));
                            }
                        })
                )
                ;

    }

    /**
     * 体力值不足
     *
     * @return
     */
    public static KumaDialog buildNoHealth(final Activity context, final int num) {
        TextView textView = new TextView(context);
        textView.setTextColor(Color.WHITE);
        textView.setText("晒红包" + "(" + num + "/1)");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 28);
        TextPaint tp = textView.getPaint();
        tp.setFakeBoldText(true);

        return new KumaDialog(context, 610, 698)
                .setBackgound(R.drawable.tilizhibuzu_tanchaung)
                .addWidget(new KumaDialog.DialogView(35, 35)
                        .setRight(true)
                        .setMarginTop(20)
                        .setMarginRight(20)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                )
                .addWidget(new KumaDialog.DialogView(textView)
                        .setBottom(true)
                        .setMarginBottom(53)
                        .setCenterHorizontal(true)
                )
                //邀请一名好友
                .addWidget(new KumaDialog.DialogView(260, 220)
                        .setMarginTop(178)
                        .setMarginLeft(30)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.SHOW_INVITEDICIPLE, "KumaDialogFactory"), "MainActivity");
                            }
                        })
                )
                //分享并被有效阅读
                .addWidget(new KumaDialog.DialogView(260, 220)
                        .setMarginTop(178)
                        .setRight(true)
                        .setMarginRight(30)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.SHOW_ARTICLE, "KumaDialogFactory"), "MainActivity");
                            }
                        })
                )
                //晒红包
                .addWidget(new KumaDialog.DialogView(260, 220)
                        .setMarginBottom(40)
                        .setBottom(true)
                        .setCenterHorizontal(true)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (num == 0) {
                                    ShareUtils.shareHongBao(context, WechatMoments.NAME);
                                } else {
                                    KumaToast.show(context, "今日已晒红包");
                                }
                            }
                        })
                )
                ;

    }

    /**
     * 简略的分享
     */
    public static KumaDialog buildShareDialog(final Activity context, View.OnClickListener onClickWechat, View.OnClickListener onClickMoment, View.OnClickListener onClickQQ) {
        return new KumaDialog(context, 610, 492)
                .setBackgound(R.drawable.fenxiangweixinpengyouquan1)
                .addWidget(new KumaDialog.DialogView(156, 204)
                        .setMarginLeft(30)
                        .setMarginTop(78)
                        .setOnClickListener(onClickWechat)
                )
                .addWidget(new KumaDialog.DialogView(156, 204)
                        .setMarginTop(78)
                        .setCenterHorizontal(true)
                        .setOnClickListener(onClickMoment)
                )
                .addWidget(new KumaDialog.DialogView(156, 204)
                        .setRight(true)
                        .setMarginRight(30)
                        .setMarginTop(78)
                        .setOnClickListener(onClickQQ)
                )
                .addWidget(new KumaDialog.DialogView(76, 76)
                        .setMarginTop(422)
                        .setMarginLeft(266)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        })
                );
    }

    public static KumaDialog buildGetNoHongBaoRainDialog(Activity context) {
        return new KumaDialog(context, 750, 1334)
                .setBackgound(R.drawable.henyihan)
                .addWidget(new KumaDialog.DialogView(50, 50)
                        .setRight(true)
                        .setMarginRight(70)
                        .setMarginTop(70)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                );
    }

}
